package com.wkn.wknAdmin.controller;

import com.wkn.wknAdmin.entity.Role;
import com.wkn.wknAdmin.entity.User;
import com.wkn.wknAdmin.result.ResultVO;
import com.wkn.wknAdmin.service.RoleMenuService;
import com.wkn.wknAdmin.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title:
 * @Description:
 * @Author: WKN
 * CreateDate: 2022/8/13 11:40
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private RoleMenuService roleMenuService;

    ResultVO result = new ResultVO();


    /**
     * 添加或更新用户
     *
     * @param role
     * @return
     */
    @PostMapping("/save")
    public ResultVO save(@RequestBody Role role) {
        return roleService.save(role);
    }



    /**
     * 通过前端传数据进行分页查询
     * 获取记录总数
     *
     * @param pageNum  当前的页码数
     * @param pageSize 每页显示的数据条数
     * @return
     */
    @GetMapping("/page")
    public ResultVO findPage(@RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("rname") String rname) {
        pageNum = (pageNum - 1) * pageSize;
        List<Role> data = roleService.selectPage(pageNum, pageSize, rname);
        int totalCount = roleService.getTotalCount(rname);
        Map<String, Object> map = new HashMap<>();
        map.put("data", data);
        map.put("totalCount", totalCount);
        return result.Success(map);
    }


    /**
     *   绑定角色和菜单的关系
     * @param roleId   角色id
     * @param menuIds   前端选择的菜单的id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public ResultVO setRoleMenu(@PathVariable("roleId") Integer roleId,
                             @RequestBody List<Integer> menuIds) {
        roleMenuService.setRoleMenu(roleId, menuIds);
        return result.Success("成功");
    }



    @GetMapping("/roleMenu/{roleId}")
    public ResultVO getRoleMenu(@PathVariable("roleId") Integer roleId) {
        List<Integer> roleMenu = roleMenuService.getRoleMenu(roleId);
        return result.Success(roleMenu);
    }



    @GetMapping("/findAll")
    public ResultVO findAll() {
        List<Role> all = roleService.findAll();
        return result.Success(all);
    }


}
